package bs.app.sms;

import bs.app.Util.Action;
import bs.app.android.Static;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.util.EventLog.Event;
import android.widget.Toast;


/**
 * Prompts for text and sends it via an SMS MessageConnection
 */
public class SMSSender {

	private String destinationAddress;
	private String data;
	private Event sendSuccessAction, sendFailAction;
	private Action action;
    public static String sInfo=""; 
	public SMSSender(String data, String destinationAddress, Event good, Event fail) {
		this.data = data;
		this.destinationAddress = destinationAddress;
		sendSuccessAction = good;
		sendFailAction = fail;
	}
	
	public SMSSender(String data, String destinationAddress, Action action) {
		this.data = data;
		this.destinationAddress = destinationAddress;
		this.action = action; 
	}



	public void sendSMS() {
		try{
			String SENT = "SMS_SENT";
	        String DELIVERED = "SMS_DELIVERED";
	 
	        PendingIntent sentPI = PendingIntent.getBroadcast(Static.App, 0,new Intent(SENT), 0);
	        PendingIntent deliveredPI = PendingIntent.getBroadcast(Static.App, 0, new Intent(DELIVERED), 0);
	 
	      //---when the SMS has been sent---
	        Static.App.registerReceiver(new BroadcastReceiver(){
	            @Override
	            public void onReceive(Context arg0, Intent arg1) {
	                switch (getResultCode())
	                {
	                    case Activity.RESULT_OK:
	                    	////System.out.println("SMS sent");
	                        Toast.makeText(Static.App.getBaseContext(), "Đang gửi tin nhắn", Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
	                    	////System.out.println("Generic failure");
	                        Toast.makeText(Static.App.getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_NO_SERVICE:
	                    	////System.out.println("No service");
	                        Toast.makeText(Static.App.getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_NULL_PDU:
	                    	////System.out.println("Null PDU");
	                        Toast.makeText(Static.App.getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
	                        break;
	                    case SmsManager.RESULT_ERROR_RADIO_OFF:
	                    	////System.out.println("Radio off");
//	                        Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
	                        break;
	                }
	            }
	        }, new IntentFilter(SENT));
	 
	        //---when the SMS has been delivered---
	        Static.App.registerReceiver(new BroadcastReceiver(){
	            @Override
	            public void onReceive(Context arg0, Intent arg1) {
	                switch (getResultCode())
	                {
	                    case Activity.RESULT_OK:
	                    	////System.out.println("Send sms thanh cong ++++++++");
	                    	Toast.makeText(Static.App.getBaseContext(), "Gửi tin nhắn thành công", Toast.LENGTH_SHORT).show();	                                
//	                    	sendSuccessAction.action();
	                        break;
	                    case Activity.RESULT_CANCELED:
	                    	Toast.makeText(Static.App.getBaseContext(), "Gửi tin nhắn không thành công", Toast.LENGTH_SHORT).show();
//	                    	sendFailAction.action();
	                        break;                        
	                }
	            }
	        }, new IntentFilter(DELIVERED));        
	 
	        SmsManager sms = SmsManager.getDefault();
	    	String address = /*"sms://" +*/ this.destinationAddress;
	        ////System.out.println("address:"+ address + " data:"+ data);
	        sms.sendTextMessage(address, null, data, sentPI, deliveredPI);     
		}catch (Exception e) {
			// TODO: handle exception
			sInfo ="Gửi tin nhắn không thành công!";
			////System.out.println("Khong gui duoc tin nhan .....................");
		}
		    
	}

	public static void sendSMSToConfirmReward(String sms, String destinationAddress, final Event actionAfterConfirm, final Event actionFailAfterConfirm) {
		SMSSender sender = new SMSSender(sms, destinationAddress, actionAfterConfirm, actionFailAfterConfirm);
        sender.sendSMS();
	}
	public static void sendSMSToConfirmReward(String sms, String destinationAddress, final Action actionFailAfterConfirm) {
		SMSSender sender = new SMSSender(sms, destinationAddress, actionFailAfterConfirm);
        sender.sendSMS();
	}
}
